package cn.com.backstage.controller.base;
import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import cn.com.api.entity.ApiT_goods_tags;
import cn.com.api.service.ApiT_goods_tagsService;
import cn.com.util.*;

public class T_goods_tagsBaseController {

	private static Logger logger = Logger.getLogger(T_goods_tagsBaseController.class);

	@Autowired
	private ApiT_goods_tagsService apiT_goods_tagsService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_tags-list")
	public ModelAndView t_goods_tags_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_goods_tags_list.jsp");

		List<ApiT_goods_tags> t_goods_tagss = apiT_goods_tagsService.findT_goods_tagss();
		mv.addObject("t_goods_tagss", t_goods_tagss);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_tags-add-pre")
	public ModelAndView t_goods_tags_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_goods_tags_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_goods_tags t_goods_tags = apiT_goods_tagsService.findT_goods_tagsById(Integer.valueOf(id));
			mv.addObject("t_goods_tags", t_goods_tags);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_tags-add")
	public void t_goods_tags_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String gtag_id = request.getParameter("gtag_id");
		String gtag_title = request.getParameter("gtag_title");
		String gtag_img = request.getParameter("gtag_img");
		String gtag_desc = request.getParameter("gtag_desc");
		String is_use = request.getParameter("is_use");
		if (is_use == null || "".equals(is_use)) {
			is_use = "1";// 启用
		}
		// String ext_1 = request.getParameter("ext_1");
		// String ext_2 = request.getParameter("ext_2");
		// String ext_3 = request.getParameter("ext_3");
		// String ext_4 = request.getParameter("ext_4");
		// String ext_5 = request.getParameter("ext_5");

		Map<String, Object> map = new HashMap<String, Object>();
		// add
		if (gtag_id == null || (gtag_id.trim()).equals("")) {
			ApiT_goods_tags t_goods_tags = new ApiT_goods_tags();
			t_goods_tags.setGtag_title(gtag_title);
			t_goods_tags.setGtag_img(gtag_img);
			t_goods_tags.setGtag_desc(gtag_desc);
			t_goods_tags.setIs_use(Integer.valueOf(is_use));
			// t_goods_tags.setExt_1(ext_1);
			// t_goods_tags.setExt_2(ext_2);
			// t_goods_tags.setExt_3(ext_3);
			// t_goods_tags.setExt_4(ext_4);
			// t_goods_tags.setExt_5(ext_5);

			apiT_goods_tagsService.insertT_goods_tags(t_goods_tags);
		}
		// edit
		else {
			ApiT_goods_tags t_goods_tags = apiT_goods_tagsService.findT_goods_tagsById(Integer.valueOf(gtag_id));
			t_goods_tags.setGtag_title(gtag_title);
			t_goods_tags.setGtag_img(gtag_img);
			t_goods_tags.setGtag_desc(gtag_desc);
			t_goods_tags.setIs_use(Integer.valueOf(is_use));
			// t_goods_tags.setExt_1(ext_1);
			// t_goods_tags.setExt_2(ext_2);
			// t_goods_tags.setExt_3(ext_3);
			// t_goods_tags.setExt_4(ext_4);
			// t_goods_tags.setExt_5(ext_5);
			apiT_goods_tagsService.updateT_goods_tags(t_goods_tags);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_goods_tags-delete")
	public void t_goods_tags_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_goods_tagsService.deleteT_goods_tags(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 上传缩略图
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "goodstags-smallpic-upload")
	public void goods_smallpic_upload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ModelMap model

		String path = ImageUtils.getServletWebAppsPath(request) ;
		path= path.replaceAll("uthink-backstage","")+ImageUtils.IMAGE_ROOT+ "/goodstags/";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String sdfStr = sdf.format(new Date());
		String fileName = sdfStr + "_" + file.getOriginalFilename();

		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		String smallpic_path = "";
		Map<String, Object> map = new HashMap<String, Object>();
		// 保存
		try {
			file.transferTo(targetFile);
			smallpic_path = ImageUtils.getDataBaseImagePath() + "/goodstags/" + fileName;

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功！");
			map.put("datas", smallpic_path);

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "上传失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
